var path = require('path');
var srcUrl = path.resolve(__dirname, './src');
var webpack = require('webpack');

var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: {
        index: srcUrl + '/index/index.js',
        admin: srcUrl + '/admin/index.js',
        role: srcUrl + '/role/index.js',
        config: srcUrl + '/config/index.js',
        section: srcUrl + '/section/index.js',
        staff: srcUrl + '/staff/index.js'
    },
    output: {
        path: path.resolve(__dirname, '../../public/static/admin'),
        publicPath: '../../public/static/admin',
        filename: './js/[name].js'
    },
    module: {
        rules: [ {
            test: /\.js$/,
            use: [{
                loader: "babel-loader",
                options: { presets: ["es2015"] }
            }]
        }, {
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: [{
                    loader: 'css-loader',
                    options: {
                        // modules: true // 设置css模块化,详情参考https://github.com/css-modules/css-modules
                        //minimize: true //css压缩
                    }
                }, {
                    loader: 'postcss-loader',
                    // 在这里进行配置，也可以在postcss.config.js中进行配置，详情参考https://github.com/postcss/postcss-loader
                    options: {
                        plugins: function() {
                            return [
                                require('autoprefixer')
                            ];
                        }
                    }
                }
                ]})
        },{
            test: /\.scss$/,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: [{loader: 'css-loader', options: {
                        //minimize: true //css压缩
                    }
                }, {
                    loader: 'postcss-loader',
                    options: {
                        plugins: function() {
                            return [
                                require('autoprefixer')
                            ];
                        }
                    }
                }, 'sass-loader']
            })
        }, {
            test: /\.(png|jpg|gif|jpeg)$/, //处理css文件中的背景图片
            loader: 'url-loader?limit=1&name=images/[name].[hash:4].[ext]'
            //当图片大小小于这个限制的时候，会自动启用base64编码图片。减少http请求,提高性能
        }]
    },
    devtool: 'eval-source-map',
    devServer: {
        historyApiFallback: true,
        noInfo: true
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"development"',
            DEV: JSON.stringify(true)
        }),
        new webpack.optimize.CommonsChunkPlugin('global'),
        new ExtractTextPlugin('./css/[name].css')
    ]
};
if (process.env.NODE_ENV === 'production') {

    module.exports.devtool = 'source-map';
    module.exports.plugins = [
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            },
            DEV: JSON.stringify(false)
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            }
        }),
        new webpack.optimize.CommonsChunkPlugin('global'),
        new ExtractTextPlugin('./css/[name].css')
    ]
}